﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using doubango_rt.BackEnd;
using doubango_rt.BackEnd.OutOfProcess;

namespace WinRT.SIP.UILogic
{
    public class DoubangoServer : IDoubangoServer
    {
        private static Server _server = null;

        public Server Instance
        {
            get { return _server; }
        }

        public async Task Initialize()
        {
            await Task.Run(() =>
            {
                // Create an instance of the server in the background process.
                _server = (Server)WindowsRuntimeMarshal.GetActivationFactory(typeof(Server)).ActivateInstance();
                UpdateSipConfig();

                // Set codecs. Not part of the configuration because apply to all SIP service instances (global)
                SipService.SetCodecs(rt_tdav_codec_id_t.tdav_codec_id_all);

                // Start SIP service
                var sipService = _server.SipService;

                if (sipService != null && sipService.RegistrationState != SipSessionState.Connected)
                {
                    if (sipService.Stop(false))
                    {
                        if (sipService.Start())
                        {
                            if (sipService.Register())
                            {
                                // All ready
                            }
                        }
                    }
                }
            });
        }

        private void UpdateSipConfig()
        {
            if (_server.SipService != null)
            {
                _server.SipService.SipConfig.DisplayName = Configuration.DEFAULT_IDENTITY_DISPLAY_NAME;
                _server.SipService.SipConfig.ImpuUri = Configuration.DEFAULT_IDENTITY_IMPU;
                _server.SipService.SipConfig.ImpiString = Configuration.DEFAULT_IDENTITY_IMPI;
                _server.SipService.SipConfig.Password = Configuration.DEFAULT_IDENTITY_PASSWORD;

                _server.SipService.SipConfig.EarlyIMS = true;/* rtServiceManager.Instance.ConfigurationService.Get(Configuration.ConfFolder.NETWORK, Configuration.ConfEntry.EARLY_IMS, Configuration.DEFAULT_NETWORK_EARLY_IMS)*/;
                _server.SipService.SipConfig.RealmUri = Configuration.DEFAULT_NETWORK_REALM;
                _server.SipService.SipConfig.ProxyHost = Configuration.DEFAULT_NETWORK_PCSCF_HOST;
                _server.SipService.SipConfig.ProxyPort = (ushort)Configuration.DEFAULT_NETWORK_PCSCF_PORT;
                _server.SipService.SipConfig.Transport = Configuration.DEFAULT_NETWORK_TRANSPORT.Equals("UDP", StringComparison.CurrentCultureIgnoreCase) ? SipNetworkTransport.UDP : SipNetworkTransport.TCP;

                _server.SipService.SipConfig.PushNotificationUri = "nullptr";
                _server.SipService.SipConfig.UserAgent = String.Format("IM-client/OMA1.0 Boghe-{0}/v{1}", "WinRT", "1.0");
            }
        }
    }
}
